/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.CommandPrompt;

import Config.ConfigCtrl;
import Config.ConfigSetting;
import Control.MainControl.MainControl;
import CrlException.CrlException;
import DataAccessor.XMLAccessor;
import java.io.Console;

/**
 *
 * @author myubuntu
 */
public class ScreenControl {

    public Screen curScreen;
    MainControl mc;
    CmdPromptStatisticCtrl statisticCtrl;

    public ScreenControl() {
    }

    public void doScreen() {
        makeScreenMain();
        Option op = Option.o13;
        while (true) {
            switch (curScreen) {
                case Main:
                case Setting:
                case Run:
                case Suspend:
                    op = readOption();
                    break;
            }
            switch (curScreen) {
                case Main:
                    switch (op) {
                        case o1:
                            makeScreenRun();
                            doRun();
                            break;
                        case o2:
                            doLoadSetting();
                            makeScreenSetting();
                            break;
                        case o3:
                            System.exit(0);
                            return;
                        default:
                            break;
                    }
                    break;
                case Run:
                    switch (op) {
                        case o1:
                            doSuspend();
                            makeScreenSuspend();
                            break;
                        case o2:
                            doStop();
                            makeScreenMain();
                            break;
                        default:
                            break;
                    }
                    break;
                case Suspend:
                    switch (op) {
                        case o1:
                            doSuspend();
                            makeScreenRun();
                            break;
                        case o2:
                            doStop();
                            makeScreenMain();
                            break;
                        default:
                            break;
                    }
                    break;
                case Setting:
                    switch (op) {
                        case o1:
                            makeScreenSettingMaxThread();
                            break;
                        case o2:
                            makeScreenSettingDepth();
                            break;
                        case o3:
                            makeScreenSettingHostMySQL();
                            break;
                        case o4:
                            makeScreenSettingDBName();
                            break;
                        case o5:
                            makeScreenSettingUsername();
                            break;
                        case o6:
                            makeScreenSettingPassWord();
                            break;
                        case o7:
                            makeScreenSettingExtractorJarFile();
                            break;
                        case o8:
                            makeScreenSettingDownLoadImage();
                            break;
                        case o9:
                            makeScreenSettingUseProxy();
                            break;
                        case o10:
                            makeScreenSettingImgDir();
                            break;
                        case o11:
                            makeScreenSettingRestartCrawling();
                            break;
                        case o12:
                            doSaveSetting();
                            makeScreenMain();
                            break;
                        default:
                            break;
                    }
                    break;
                case SettingDBName:
                case SettingDepth:
                case SettingDownLoadImage:
                case SettingExtractorJarFile:
                case SettingHostMySQL:
                case SettingImgDir:
                case SettingMaxThread:
                case SettingPassWord:
                case SettingProxyHost:
                case SettingProxyPort:
                case SettingRestartCrawling:
                case SettingUseProxy:
                case SettingUsername:
                    makeScreenSetting();
                    break;
            }


        }
    }

    public enum Screen {

        Main, Run, Suspend, Setting, SettingMaxThread, SettingDepth,
        SettingHostMySQL, SettingDBName, SettingUsername, SettingPassWord,
        SettingExtractorJarFile, SettingDownLoadImage, SettingUseProxy,
        SettingProxyHost, SettingProxyPort, SettingImgDir, SettingRestartCrawling
    }

    public enum Option {

        o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, o11, o12, o13
    }

    public void makeScreenMain() {
        curScreen = Screen.Main;
        System.out.println("Choose option below:");
        System.out.println("\t1. Run crawler");
        System.out.println("\t2. Change setting");
        System.out.println("\t3. Exit");
    }

    public void makeScreenSuspend() {
        curScreen = Screen.Suspend;
        System.out.println("\t1.Continue | 2.Stop");
    }

    public void makeScreenRun() {
        curScreen = Screen.Run;
        System.out.println("Crawler is running");
        System.out.println("\t1. Pause | 2. Stop : ");
    }

    public void makeScreenSetting() {
        curScreen = Screen.Setting;
        System.out.println("Setting Crawler");
        System.out.println("1. Max thread : " + ConfigSetting.maxThread);
        System.out.println("2. Depth : " + ConfigSetting.depth);
        System.out.println("3. Host MySQL : " + ConfigSetting.hostMySQL);
        System.out.println("4. Database Name : " + ConfigSetting.databaseName);
        System.out.println("5. Username : " + ConfigSetting.username);
        System.out.println("6. Password : " + ConfigSetting.password);
        System.out.println("7. Extractor path : " + ConfigSetting.ExtractorJarFile);
        System.out.println("8. Download image : " + ConfigSetting.downloadImage);
        System.out.print("9. Use proxy : " + ConfigSetting.useProxy);
        if (ConfigSetting.useProxy) {
            System.out.println(" - Host : " + ConfigSetting.proxyHost + " - Post : " + ConfigSetting.proxyPort);
        } else {
            System.out.println("");
        }
        System.out.println("10. Image Path : " + ConfigSetting.imageDir);
        System.out.println("11. Restart Crawling : " + ConfigSetting.restartCrawling);
        System.out.println("12. Exit Setting");
    }

    public void makeScreenSettingMaxThread() {
        curScreen = Screen.SettingMaxThread;
        System.out.println("Setting Max Thread");
        System.out.println("Old config : " + ConfigSetting.maxThread);
        System.out.print("New config : ");
        boolean done = false;
        int output = 0;
        while (!done) {
            done = true;
            String out = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                output = cc.checkValidMaxThread(out);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.maxThread = output;
    }

    public void makeScreenSettingDepth() {
        curScreen = Screen.SettingDepth;
        System.out.println("Setting URL Depth");
        System.out.println("Old config : " + ConfigSetting.depth);
        System.out.print("New config : ");
        boolean done = false;
        int output = 0;
        while (!done) {
            done = true;
            String out = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                output = cc.checkValidDepth(out);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.depth = output;
    }

    public void makeScreenSettingHostMySQL() {
        curScreen = Screen.SettingHostMySQL;
        System.out.println("Setting Host MySQL");
        System.out.println("Old config : " + ConfigSetting.hostMySQL);
        System.out.print("New config : ");
        ConfigSetting.hostMySQL = readString();
    }

    public void makeScreenSettingDBName() {
        curScreen = Screen.SettingDBName;
        System.out.println("Setting Database Name");
        System.out.println("Old config : " + ConfigSetting.databaseName);
        System.out.print("New config : ");
        ConfigSetting.databaseName = readString();
    }

    public void makeScreenSettingUsername() {
        curScreen = Screen.SettingUsername;
        System.out.println("Setting Username");
        System.out.println("Old config : " + ConfigSetting.username);
        System.out.print("New config : ");
        ConfigSetting.username = readString();
    }

    public void makeScreenSettingPassWord() {
        curScreen = Screen.SettingPassWord;
        System.out.println("Setting Password");
        System.out.println("Old config : " + ConfigSetting.password);
        System.out.print("New config : ");
        ConfigSetting.password = readString();
    }

    public void makeScreenSettingExtractorJarFile() {
        curScreen = Screen.SettingExtractorJarFile;
        System.out.println("Setting Extractor Path");
        System.out.println("Old config : " + ConfigSetting.ExtractorJarFile);
        System.out.print("New config : ");
        boolean done = false;
        String output = null;
        while (!done) {
            done = true;
            output = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                cc.checkValidExtractor(output);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.ExtractorJarFile = output;
    }

    public void makeScreenSettingDownLoadImage() {
        curScreen = Screen.SettingDownLoadImage;
        System.out.println("Setting Download Image");
        System.out.println("Old config : " + ConfigSetting.downloadImage);
        System.out.print("New config : ");
        boolean done = false;
        boolean output = false;
        while (!done) {
            done = true;
            String out = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                output = cc.checkValidBoolean(out);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.downloadImage = output;
    }

    public void makeScreenSettingUseProxy() {
        curScreen = Screen.SettingUseProxy;
        System.out.println("Setting Use proxy");
        System.out.println("Old config : " + ConfigSetting.useProxy);
        System.out.print("New config : ");
        boolean done = false;
        boolean output = false;
        while (!done) {
            done = true;
            String out = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                output = cc.checkValidBoolean(out);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.useProxy = output;
        //
        if (ConfigSetting.useProxy) {
            System.out.println("Setting Proxy Host");
            System.out.println("Old config : " + ConfigSetting.proxyHost);
            System.out.print("New config : ");
            ConfigSetting.proxyHost = readString();
            System.out.println("Setting Proxy Port");
            System.out.println("Old config : " + ConfigSetting.proxyPort);
            System.out.print("New config : ");
            done = false;
            int outPort = 0;
            while (!done) {
                done = true;
                String out = readString();
                ConfigCtrl cc = new ConfigCtrl();
                try {
                    outPort = cc.checkValidPort(out);
                } catch (CrlException ex) {
                    done = false;
                    System.out.println(ex.toString());
                    System.out.print("New config : ");
                }
            }
            ConfigSetting.proxyPort = outPort + "";
        }
    }

    public void makeScreenSettingImgDir() {
        curScreen = Screen.SettingImgDir;
        System.out.println("Setting Image Path");
        System.out.println("Old config : " + ConfigSetting.imageDir);
        System.out.print("New config : ");
        boolean done = false;
        String output = null;
        while (!done) {
            done = true;
            output = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                cc.checkImageDir(output);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.imageDir = output;
    }

    public void makeScreenSettingRestartCrawling() {
        curScreen = Screen.SettingRestartCrawling;
        System.out.println("Setting Restart Crawling");
        System.out.println("Old config : " + ConfigSetting.restartCrawling);
        System.out.print("New config : ");
        boolean done = false;
        boolean output = false;
        while (!done) {
            done = true;
            String out = readString();
            ConfigCtrl cc = new ConfigCtrl();
            try {
                output = cc.checkValidBoolean(out);
            } catch (CrlException ex) {
                done = false;
                System.out.println(ex.toString());
                System.out.print("New config : ");
            }
        }
        ConfigSetting.restartCrawling = output;
    }

    public String readString() {
        Console console = System.console();
        String out = null;
        while (out == null) {
            out = console.readLine();
        }
        return out;
    }

    public Option readOption() {
        Console console = System.console();
        boolean done = false;
        String line = null;
        while (!done) {
            line = console.readLine();
            if (line != null) {
                if (line.equals("1")) {
                    return Option.o1;
                }
                if (line.equals("2")) {
                    return Option.o2;
                }
                if (line.equals("3")) {
                    return Option.o3;
                }
                if (line.equals("4")) {
                    return Option.o4;
                }
                if (line.equals("5")) {
                    return Option.o5;
                }
                if (line.equals("6")) {
                    return Option.o6;
                }
                if (line.equals("7")) {
                    return Option.o7;
                }
                if (line.equals("8")) {
                    return Option.o8;
                }
                if (line.equals("9")) {
                    return Option.o9;
                }
                if (line.equals("10")) {
                    return Option.o10;
                }
                if (line.equals("11")) {
                    return Option.o11;
                }
                if (line.equals("12")) {
                    return Option.o12;
                }
                if (line.equals("13")) {
                    return Option.o13;
                }
            }
        }
        return Option.o13;
    }

    public void doRun() {
        try {
            mc = new MainControl();
            mc.innitiate();
            statisticCtrl = new CmdPromptStatisticCtrl();
            mc.startWork();
            statisticCtrl.start();
        } catch (CrlException ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void doSuspend() {
        try {
            mc.suspendWork();
            statisticCtrl.suspendWork();
        } catch (CrlException ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void doStop() {
        try {
            mc.stopWork();
            statisticCtrl.StopThead();
        } catch (CrlException ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void doLoadSetting() {
        try {
            XMLAccessor.ReadConfigXML();
        } catch (CrlException ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void doSaveSetting() {
        try {
            XMLAccessor.WriteConfigXML();
        } catch (CrlException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
